package cn.wolfcode.web.controller;

import cn.wolfcode.queryobject.CustomerReportObject;
import cn.wolfcode.queryobject.QueryObject;
import cn.wolfcode.service.ICustomerReportservice;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportservice customerReportservice;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerReportObject qo, Model model){
        PageInfo PageInfo = customerReportservice.query(qo);
        model.addAttribute("PageInfo",PageInfo);
        return "/customerReport/list";
    }

    @RequestMapping("/listbar")
    public String listbar(@ModelAttribute("qo") CustomerReportObject qo, Model model){
        // 统计报表信息,不需要分页
        List<HashMap> list = customerReportservice.listAll(qo);
        // x轴数据
        ArrayList xList = new ArrayList<>();
        // y轴数据
        ArrayList yList = new ArrayList<>();

        for (HashMap map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        // //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        // 需要将xList对象转换成字符串,  不能用list集合的toString()方法,因为是不带双引号的.需要使用Json.toString()方法
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));

        //分组类型转为文字显示
        String groupTypeName = MessageUtil.changMsg(qo);
        model.addAttribute("groupTypeName",groupTypeName);
        return "/customerReport/bar";
    }

    @RequestMapping("/listpie")
    public String listPie(@ModelAttribute("qo") CustomerReportObject qo, Model model){
        // 统计报表信息,不需要分页
        List<HashMap> list = customerReportservice.listAll(qo);
        // legend 数据
        ArrayList legendList = new ArrayList<>();
        // series 数据
        ArrayList seriesList = new ArrayList<>();
        for (HashMap map : list) {
            legendList.add(map.get("groupType"));
            // 通过map获取数据,封装为新的map对象,里面的key需要有name和value的
            HashMap temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("number"));
            seriesList.add(temp);
        }

        // 需要将xList对象转换成字符串,  不能用list集合的toString()方法,因为是不带双引号的.需要使用Json.toString()方法
        model.addAttribute("legendList", JSON.toJSONString(legendList));
        model.addAttribute("seriesList",JSON.toJSONString(seriesList));

        String groupTypeName = MessageUtil.changMsg(qo);
        model.addAttribute("groupTypeName",groupTypeName);
        return "/customerReport/pie";
    }
}
